package util;

import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * XML Builder 转换工具
 * @author Administrator
 *
 */
public class XmlBuilder {
	/**
	 * @param clazz
	 * @param xmlStr
	 * @return
	 * @throws JAXBException
	 */
	/**
	 * @param clazz
	 * @param xmlStr
	 * @return
	 * @throws JAXBException
	 * @throws IOException 
	 */
	public static Object xmlStrToObject(Class<?> clazz,String xmlStr) throws JAXBException, IOException {
		Object xmlObject=null;
		Reader  reader =null;
		JAXBContext context = JAXBContext.newInstance(clazz);

		//XML 转换对象的接口
		Unmarshaller unmarshaller = context.createUnmarshaller();
		reader = new StringReader(xmlStr);
		xmlObject = unmarshaller.unmarshal(reader);

		if(null !=reader) {
			reader.close();
		}
		return xmlObject;

	}

}
